/************************************************************************/
/*    Author: Scott Bevin                                               */
/************************************************************************/

#include "PhysicsWorld.h"

namespace XF
{
	PhysicsWorld::PhysicsWorld(float x, float y, float w, float h)
	{
		PhysicsWorld(x, y, w, h, 10, 100);
	}

	PhysicsWorld::PhysicsWorld(float x, float y, float w, float h, unsigned int targetQuadDepth, unsigned int targetQuadObjects)
	{
		mQuadTree = new QuadTree(x, y, w, h, targetQuadDepth, targetQuadObjects);
	}

	PhysicsWorld::~PhysicsWorld()
	{

	}

	void PhysicsWorld::Update(double dT)
	{

	}

	Body* PhysicsWorld::CreateBody(XF::Vector3 pos, float radius)
	{
		Body* newBody = new Body(pos, radius);

		mAllBodies.push_back(newBody);
		mQuadTree->AddBody(newBody);

		return newBody;
	}

	void PhysicsWorld::RemoveBody(Body* body)
	{
		mAllBodies.remove(body);
		mQuadTree->RemoveBody(body);
	}

	void PhysicsWorld::UpdateAllBodies(double dT)
	{
		for(std::list<Body*>::iterator it = mAllBodies.begin();
			it != mAllBodies.end(); ++it)
		{
			Body* body = *it;

			body->Position(body->Position() + (body->Velocity() * dT));

			if(body->HasMoved())
			{
				mQuadTree->RemoveBody(body);
				mQuadTree->AddBody(body);
			}
		}
	}
}